package com.example.service.impl;

import com.example.Pojo.Emp;
import com.example.Pojo.PageBean;
import com.example.mapper.EmpMapper;
import com.example.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    /*@Override
    public PageBean page(Integer page, Integer pageSize) {
        //1,获取总记录数
        Long count = empMapper.count();

        //2,获取分页查询结果列表
        //利用公式计算起始索引
        Integer start = (page - 1) * pageSize;
        List<Emp> empList = empMapper.page(start,pageSize);

        //3, 封装PageBean对象
        PageBean pageBean = new PageBean(count, empList);
        return pageBean;
    }*/

    //修改代码
    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Integer gender, LocalDate begin, LocalDate end) {
        //1,获取分页参数
        //这里通过PageHelper来调用.startPage(参数一: 页码数, 参数二: 展现的数据)
        PageHelper.startPage(page, pageSize);

        //2, 执行查询
        //这里直接调用empMapper里的list()方法
        List<Emp> empList = empMapper.list(name, gender, begin, end);
        //然后将其强转为Page类型,泛型就是Emp
        Page<Emp> p = (Page<Emp>) empList;


        //3, 封装PageBean对象
        //这里的参数调用p.getTotal()来获得总记录数,调用p.getResult()来获取结果列表,就是每页也要显示几个数据
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    //根据id删除员工
    @Override
    public void deleteById(List<Integer> ids) {
        empMapper.delete(ids);
    }

    //新增员工
    @Override
    public void insert(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }

    //修改员工
    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateEmp(emp);
    }

    //根据id查询员工信息
    @Override
    public Emp selectOne(Integer id) {

        return empMapper.selectOne(id);
    }

    @Override
    public Emp login(Emp emp) {
        return empMapper.login(emp);
    }


}
